package data.account.attendee;

import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;

public class AttendeeTable extends JTable
{
	private static final long serialVersionUID = 4964642400546828397L;

	private ArrayList<AttendeeAccount> attendees;
	public ArrayList<AttendeeAccount> attendees() { return attendees; }

	private Model attendeeModel;
	String[][] data;

	public AttendeeTable(ArrayList<AttendeeAccount> attendees)
	{
		super(new String[0][3], new String[] { "Full Name", "User ID", "Organization" });
		
		this.attendees = attendees;
		Collections.sort(attendees);
		data = new String[attendees.size()][3];
		for(int i = 0; i < attendees.size(); i++)
		{
			AttendeeAccount attendee = attendees.get(i);
			data[i][0] = attendee.fullName();
			data[i][1] = attendee.userID();
			data[i][2] = attendee.org();
		}
		setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		setModel(attendeeModel = new Model());
		getColumnModel().getColumn(0).setHeaderValue("Full Name");
		getColumnModel().getColumn(1).setHeaderValue("User ID");
		getColumnModel().getColumn(2).setHeaderValue("Organization");
	}
	
	public void updateContents(int row, int column)
	{
		Collections.sort(attendees);
		data = new String[attendees.size()][3];
		for(int i = 0; i < attendees.size(); i++)
		{
			AttendeeAccount attendee = attendees.get(i);
			data[i][0] = attendee.fullName();
			data[i][1] = attendee.userID();
			data[i][2] = attendee.org();
		}
		attendeeModel.fireTableDataChanged();
		if(row != -1 && column != -1)
		{
			row = Math.max(0, Math.min(row, getRowCount() - 1));
			setRowSelectionInterval(row, row);
			setColumnSelectionInterval(column, column);
		}
		validate();
		repaint();
	}

	public AttendeeAccount getSelected()
	{
		int index = getSelectedRow();
		if(index != -1 && index < attendees.size())
			return attendees.get(index);
		return null;
	}
	
	public void transferSelectedTo(AttendeeTable attendeeTable)
	{
		AttendeeAccount event = getSelected();
		if(event != null && attendees.remove(event))
		{
			attendeeTable.attendees().add(event);
			int row = getSelectedRow();
			int column = getSelectedColumn();
			updateContents(row, column);
			row = attendeeTable.getSelectedRow();
			column = attendeeTable.getSelectedColumn();
			attendeeTable.updateContents(row, column);
		}
	}
	
	public boolean removeAttendee(AttendeeAccount attendee)
	{
		if(attendees.remove(attendee))
		{
			int row = getSelectedRow();
			int column = getSelectedColumn();
			updateContents(row, column);
			return true;
		}
		return false;
	}
	
	public boolean addAttendee(AttendeeAccount attendee)
	{
		if(!attendees.contains(attendee))
		{
			attendees.add(attendee);
			int row = getSelectedRow();
			int column = getSelectedColumn();
			updateContents(row, column);
			return true;
		}
		return false;
	}
	
	private class Model extends AbstractTableModel
	{
		private static final long serialVersionUID = -6279807964207409321L;
		
		@Override public int getColumnCount() { return 3; }
		@Override public int getRowCount() { return data.length; }
		@Override public Object getValueAt(int row, int column) { return data[row][column]; }
	}
}